package com.skp.shaphan.actions;

import java.sql.Connection;

import com.skp.shaphan.ui.ConnectRunnable;
import com.skp.shaphan.ui.connectioncontrols.SavedConnectionOptions;
import com.skp.shaphan.ui.dialogs.ConnectionDialog;

/**
 * Creates a connection to a database
 * @author Stephen Phillips
 *
 */
public class SavedConnectionAction implements ConnectRunnable {
	private ConnectionDialog dialog;
	private SavedConnectionOptions gui;
	
	/**
	 * Creates the new action attached to a specific dialog
	 * @param dialog the dialog containing the connection information
	 */
	public SavedConnectionAction(ConnectionDialog dialog, SavedConnectionOptions gui) {
		this.dialog = dialog;
		this.gui = gui;
	}
	
	/**
	 * Creates the connection and attaches it to the SQL Editor
	 */
	@Override
	public void run() {
		if(gui.getSelectedIndex() < 0) {
			System.out.println("Select a connection!");
			return;
		}
		dialog.changeToTab(gui.getSelectedConnectionClass());
		dialog.getCurrentConnectionOptions().setConnectionOptions(gui.getSelectedConnectionProperties());
		dialog.setConnectionName(gui.getSelectedConnectionName());
		dialog.getOKAction().run();
	}

	@Override
	public Connection connect() {
		// not sure how to implement this -- the OK action is just a runnable -- doesn't have a connect() method
		return null;
	}

}
